package com.cpa.admin.controller;

import com.cpa.admin.config.BaseController;
import com.cpa.admin.config.WebSecurityConfig;
import com.cpa.admin.model.ProductDo;
import com.cpa.admin.service.ProductService;
import com.cpa.admin.vo.PageVo;
import com.cpa.admin.vo.ProductVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @author zhen.zhao01
 * @CreateDate 2018/10/31
 */
@Controller
@RequestMapping("product")
public class ProductController extends BaseController {

    @Value("${url}")
    private String url;

    @Autowired
    private ProductService productService;


    @GetMapping("list")
    public String getList(HttpSession session,HttpServletRequest request) {
        request.setAttribute("url",url + "login?productName=" );
        return "product/list";
    }


    @ResponseBody
    @GetMapping("data")
    public PageVo<ProductVo> getPageData(@RequestParam(required = false) String productName,Integer page, Integer limit) {

        Page<ProductVo> productVoPage = PageHelper.startPage(page, limit)
                .doSelectPage(() -> productService.findAllVo(productName));


        PageVo<ProductVo> pageVo = new PageVo<>();
        pageVo.setCode(0);
        pageVo.setCount(productVoPage.getTotal());
        pageVo.setData(productVoPage.getResult());

        return pageVo;
    }

    @GetMapping("add")
    public String addProductView() {
        return "product/add";
    }


    @PostMapping("add")
    public String addProduct(ProductDo productDo) {
        System.out.println(productDo.toString());
        productDo.setCreateBy("1");
        productDo.setStatus(1);

        productService.insertSelective(productDo);
        return "redirect:/product/list";
    }

    @ResponseBody
    @DeleteMapping("{id}")
    public void delete(@PathVariable String id) {
        ProductDo productDo = productService.selectByPrimaryKey(id);
        productDo.setDeleteFlag(1);
        productService.updateByPrimaryKeySelective(productDo);
    }


    @GetMapping("edit/{id}")
    public String getEditPage(@PathVariable String id, HttpServletRequest request) {
        ProductDo productDo = productService.selectByPrimaryKey(id);
        request.setAttribute("productDo",productDo);
        return "product/edit";
    }

    @PostMapping("update")
    public String update(ProductDo productDo) {
        ProductDo oldDo = productService.selectByPrimaryKey(productDo.getId());

        if (oldDo == null) {
            throw new RuntimeException("未找到");
        }

        oldDo.setProductName(productDo.getProductName());
        oldDo.setProductAmount(productDo.getProductAmount());
        oldDo.setRemark(productDo.getRemark());
        oldDo.setSettleTime(productDo.getSettleTime());
        oldDo.setUpdateTime(new Date());
        productService.updateByPrimaryKeySelective(oldDo);
        return "redirect:/product/list";
    }

}
